/*    */ package com.newcrm.webapi.sub;
/*    */ 
/*    */ import com.newcrm.bean.CompanyInfo;
/*    */ import com.newcrm.bean.User;
/*    */ import com.newcrm.db.UserManager;
/*    */ import com.newcrm.exception.NotFoundException;
/*    */ import com.newcrm.webapi.AbstractAction;
/*    */ import com.newcrm.webapi.AbstractHttpServlet;
/*    */ import java.io.IOException;
/*    */ import java.io.PrintWriter;
/*    */ import java.util.List;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import net.sf.json.JSONObject;
/*    */ 
/*    */ public class DeleteUser extends AbstractAction
/*    */ {
/* 27 */   private String id = "";
/*    */ 
/*    */   public DeleteUser(AbstractHttpServlet servlet) {
/* 30 */     super(servlet);
/*    */   }
/*    */ 
/*    */   public DeleteUser(AbstractHttpServlet servlet, String id) {
/* 34 */     super(servlet);
/* 35 */     this.id = id;
/*    */   }
/*    */ 
/*    */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*    */     throws IOException, ServletException, NotFoundException
/*    */   {
/* 41 */     response.setContentType("text/html;charset=utf-8");
/* 42 */     PrintWriter out = response.getWriter();
/* 43 */     JSONObject jsonObject = new JSONObject();
/* 44 */     UserManager manager = new UserManager();
/* 45 */     User u = super.getLoginUser(request);
/* 46 */     if ((!u.isAdmin) && (!u.isGroupAdmin)) {
/* 47 */       super.showErrorMessage(response, "没有删除用户的权限");
/* 48 */       return;
/*    */     }
/*    */ 
/* 52 */     if (!u.isAdmin) {
/* 53 */       CompanyInfo company = super.getCompany(request);
/* 54 */       User deleteUser = company.getUser(this.id);
/* 55 */       if (deleteUser == null) {
/* 56 */         super.showErrorMessage(response, "删除用户失败，请稍后重试");
/* 57 */         return;
/*    */       }
/*    */ 
/* 60 */       if (!company.isGroupAdmin(u, deleteUser.groupId)) {
/* 61 */         super.showErrorMessage(response, "没有删除用户的权限");
/* 62 */         return;
/*    */       }
/*    */     }
/*    */ 
/* 66 */     int result = manager.delete(this.id);
/* 67 */     if (result <= 0) {
/* 68 */       super.showErrorMessage(response, "删除用户失败，请稍后重试");
/* 69 */       return;
/*    */     }
/*    */ 
/* 73 */     CompanyInfo company = super.getCompany(request);
/* 74 */     List users = company.users;
/* 75 */     for (int i = users.size() - 1; i >= 0; i--) {
/* 76 */       if (((User)users.get(i)).userId.equals(this.id)) {
/* 77 */         users.remove(i);
/* 78 */         break;
/*    */       }
/*    */     }
/*    */ 
/* 82 */     jsonObject.put("error", "0");
/* 83 */     out.println(jsonObject);
/* 84 */     out.flush();
/* 85 */     out.close();
/*    */   }
/*    */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.DeleteUser
 * JD-Core Version:    0.6.1
 */